package com.gs.synedocket.util;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.gs.synedocket.activity.LoginActivity;
import com.gs.synedocket.constant.AllConstants;
import com.gs.synedocket.service.CloseService;
import com.gs.synedocket.service.LogService;

import java.util.ArrayList;
import java.util.List;

public class ActivityUtil {
    private List<Activity> activityList;

    private static volatile ActivityUtil instance;

    public ActivityUtil() {
        activityList = new ArrayList<>();
    }

    public static ActivityUtil getInstance() {
        if (instance == null) {
            synchronized (ActivityUtil.class) {
                if (instance == null) {
                    instance = new ActivityUtil();
                }
            }
        }
        return instance;
    }

    public void addActivity(Activity activity) {
        if (!activityList.contains(activity)) {
            activityList.add(activity);
        }
    }

    public void removeActivity(Activity activity) {
        if (activityList != null && !activityList.isEmpty()) {
            activityList.remove(activity);
            activity = null;
        }
    }

    public void finishActivity(Context context) {
        NotificationUtil.getInstance().getNotificationManager().cancelAll();
        SPUtil.getInstance().putString(AllConstants.TOKEN, "");
        SPUtil.getInstance().putString(AllConstants.REFRESH_TOKEN, "");
        SPUtil.getInstance().putString(AllConstants.LOGIN_INFO, "");
        SPUtil.getInstance().putString(AllConstants.TRUCK_NAME, "");
        SPUtil.getInstance().putString(AllConstants.ID, "");
        LogService.stopLog();
        context.stopService(new Intent(context, LogService.class));
        context.stopService(new Intent(context, CloseService.class));
        if (activityList != null && !activityList.isEmpty()) {
            for (Activity activity : activityList) {
                activity.finish();
                activity = null;
            }
            activityList.clear();
        }
        Intent intent = new Intent(AppUtil.getInstance().getContext(), LoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        AppUtil.getInstance().getContext().startActivity(intent);
    }

    public boolean isSurvive(Activity activity) {
        return activity != null && !activity.isFinishing() && !activity.isDestroyed();
    }

    public boolean isTopActivity() {
        ActivityManager am = (ActivityManager) AppUtil.getInstance().getContext().getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> appProcesses = am.getRunningAppProcesses();
        if (appProcesses == null) {
            return false;
        }
        for (ActivityManager.RunningAppProcessInfo process : appProcesses) {
            if (process.processName.equals(AppUtil.getInstance().getContext().getPackageName()) &&
                    process.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                return true;
            }
        }
        return false;
    }

    public void removeChildFragment(Fragment fragment) {
        if (fragment == null) {
            return;
        }
        FragmentManager fm = fragment.getChildFragmentManager();
        removeFragment(fm);
    }

    public void removeFragment(FragmentActivity activity) {
        if (activity == null) {
            return;
        }
        FragmentManager fm = activity.getSupportFragmentManager();
        removeFragment(fm);
    }

    private void removeFragment(FragmentManager fm) {
        FragmentTransaction transaction = fm.beginTransaction();
        List<Fragment> fragments = fm.getFragments();
        if (fragments.size() != 0) {
            for (Fragment fragment : fragments) {
                transaction.remove(fragment);
            }
        }
    }
}
